from flask_login import UserMixin
from datetime import datetime
from extensions import db


class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(120), nullable=False)
    role = db.Column(db.String(20), nullable=False)
    class_name = db.Column(db.String(50))
    real_name = db.Column(db.String(50))
    academy_name = db.Column(db.String(100))
    major_name = db.Column(db.String(100))


class Evaluation(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    student_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    class_name = db.Column(db.String(50), nullable=False)
    academic_score = db.Column(db.Float)
    moral_score = db.Column(db.Float)
    physical_score = db.Column(db.Float)
    ability_score = db.Column(db.Float)
    labor_score = db.Column(db.Float)
    total_score = db.Column(db.Float)
    semester = db.Column(db.String(20))

    student = db.relationship('User', backref='evaluations')


class Scholarship(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    student_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    # 基本信息
    real_name = db.Column(db.String(50))  # 姓名
    gender = db.Column(db.String(10))  # 性别
    birth_date = db.Column(db.Date)  # 出生日期
    ethnicity = db.Column(db.String(20))  # 民族
    id_number = db.Column(db.String(18))  # 身份证号
    hometown = db.Column(db.String(100))  # 籍贯
    household_type = db.Column(db.String(20))  # 户口性质
    political_status = db.Column(db.String(50))  # 政治面貌

    # 学籍信息
    college = db.Column(db.String(100))  # 学院
    major = db.Column(db.String(100))  # 专业
    class_name = db.Column(db.String(50))  # 班级

    # 学习情况
    required_courses = db.Column(db.Integer)  # 必修课门数
    pass_courses = db.Column(db.Integer)  # 必修课及格门数
    total_students = db.Column(db.Integer)  # 总人数
    rank = db.Column(db.Integer)  # 成绩排名
    ranking_type = db.Column(db.String(20))  # 排名类型（正常/专业延长）
    rank_scope = db.Column(db.String(20))  # 排名范围（按专业/按专业方向）
    rank_description = db.Column(db.Text)  # 排名说明

    # 获奖情况
    awards = db.Column(db.JSON)  # 存储获奖记录列表

    # 申请信息
    type = db.Column(db.String(50))  # 奖学金类型
    amount = db.Column(db.Float)  # 金额
    status = db.Column(db.String(20), default='pending')  # 申请状态
    semester = db.Column(db.String(20))  # 学期
    reason = db.Column(db.Text)  # 申请理由
    proof_file = db.Column(db.String(255))  # 证明材料
    submit_time = db.Column(db.DateTime, default=datetime.now)  # 提交时间

    student = db.relationship('User', backref='scholarships')


class Outstanding(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    student_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    category = db.Column(db.String(50))  # 申请类型
    semester = db.Column(db.String(20))  # 学期
    reason = db.Column(db.Text)  # 申请理由
    proof_file = db.Column(db.String(255))  # 证明材料
    status = db.Column(db.String(20), default='pending')  # 申请状态
    submit_time = db.Column(db.DateTime, default=datetime.now)  # 提交时间
    update_time = db.Column(db.DateTime)  # 审核时间

    student = db.relationship('User', backref='outstandings')


class AcademicRecord(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    student_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    class_name = db.Column(db.String(50), nullable=False)
    semester = db.Column(db.String(20), nullable=False)
    academic_score = db.Column(db.Float, nullable=False)
    proof_file = db.Column(db.String(255))
    status = db.Column(db.String(20), default='pending')
    submit_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)

    student = db.relationship('User', backref='academic_records')


class MoralRecord(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    student_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    class_name = db.Column(db.String(50), nullable=False)
    semester = db.Column(db.String(20), nullable=False)
    base_score = db.Column(db.Float, nullable=False, default=60.0)
    bonus_items = db.Column(db.JSON)
    penalty_items = db.Column(db.JSON)
    total_score = db.Column(db.Float)
    proof_file = db.Column(db.String(255))
    status = db.Column(db.String(20), default='pending')
    submit_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)

    student = db.relationship('User', backref='moral_records')


class PhysicalRecord(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    student_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    class_name = db.Column(db.String(50), nullable=False)
    semester = db.Column(db.String(20), nullable=False)
    course_type = db.Column(db.String(10), nullable=False)
    test_result = db.Column(db.String(20))
    physical_score = db.Column(db.Float, nullable=False)
    proof_file = db.Column(db.String(255))
    status = db.Column(db.String(20), default='pending')
    submit_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)

    student = db.relationship('User', backref='physical_records')


class AbilityRecord(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    student_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    class_name = db.Column(db.String(50), nullable=False)
    semester = db.Column(db.String(20), nullable=False)

    # 学术科技能力项目和分数
    academic_items = db.Column(db.JSON)  # 存储项目列表
    academic_score = db.Column(db.Float)  # 学术科技能力总分

    # 社会实践能力项目和分数
    social_items = db.Column(db.JSON)  # 存储项目列表
    social_score = db.Column(db.Float)  # 社会实践能力总分

    # 综合技能项目和分数
    skill_items = db.Column(db.JSON)  # 存储项目列表
    skill_score = db.Column(db.Float)  # 综合技能总分

    total_score = db.Column(db.Float)  # 能力素质总分（三项之和）
    proof_file = db.Column(db.String(255))
    status = db.Column(db.String(20), default='pending')
    submit_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)

    student = db.relationship('User', backref='ability_records')


class LaborRecord(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    student_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    class_name = db.Column(db.String(50), nullable=False)
    semester = db.Column(db.String(20), nullable=False)
    labor_type = db.Column(db.String(10), nullable=False)  # E1, E2, E3
    labor_score = db.Column(db.Float, nullable=False)
    proof_file = db.Column(db.String(255))
    status = db.Column(db.String(20), default='pending')
    submit_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)

    student = db.relationship('User', backref='labor_records')


class ScholarshipApplication(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    submit_time = db.Column(db.DateTime, default=datetime.utcnow)
    basic_info = db.Column(db.JSON)
    study_info = db.Column(db.JSON)
    scholarship_info = db.Column(db.JSON)
    awards_info = db.Column(db.JSON)
    application_reason = db.Column(db.Text)
    attachments = db.Column(db.JSON)
    status = db.Column(db.String(20), default='pending')
    class_name = db.Column(db.String(50))

    user = db.relationship('User', backref='scholarship_applications')

    def __init__(self, **kwargs):
        super(ScholarshipApplication, self).__init__(**kwargs)